What is nice-grpc-common?
The nice-grpc-common package provides common utilities and types for building gRPC services with the nice-grpc framework. It simplifies the process of creating and managing gRPC services in Node.js, offering a more user-friendly API compared to the standard grpc package.
What are nice-grpc-common's main functionalities?
Creating a gRPC Service
This code demonstrates how to create a simple gRPC service using nice-grpc-common. The service has a single method, sayHello, which returns a greeting message.
const { createServer } = require('nice-grpc');
const { createServiceDefinition } = require('nice-grpc-common');
const serviceDefinition = createServiceDefinition({
sayHello: async (request) => {
return { message: `Hello, ${request.name}!` };
},
});
const server = createServer();
server.add(serviceDefinition);
server.listen('0.0.0.0:50051');
Client Interceptors
This code demonstrates how to create and use a client interceptor with nice-grpc-common. The interceptor logs the method being called before proceeding with the gRPC call.
const { createChannel, createClient } = require('nice-grpc');
const { createClientInterceptor } = require('nice-grpc-common');
const loggingInterceptor = createClientInterceptor(async (call, method, options) => {
console.log(`Calling method: ${method.path}`);
return call(method, options);
});
const channel = createChannel('localhost:50051');
const client = createClient(serviceDefinition, channel, { interceptors: [loggingInterceptor] });
client.sayHello({ name: 'World' }).then(response => {
console.log(response.message);
});
Server Interceptors
This code demonstrates how to create and use a server interceptor with nice-grpc-common. The interceptor logs the method being called before proceeding with the gRPC call.
const { createServer } = require('nice-grpc');
const { createServerInterceptor } = require('nice-grpc-common');
const loggingInterceptor = createServerInterceptor(async (call, method, options) => {
console.log(`Received call to method: ${method.path}`);
return call(method, options);
});
const server = createServer({ interceptors: [loggingInterceptor] });
server.add(serviceDefinition);
server.listen('0.0.0.0:50051');
Other packages similar to nice-grpc-common
grpc
The grpc package is the official Node.js implementation of gRPC. It provides a comprehensive set of features for building gRPC services but has a more complex and less user-friendly API compared to nice-grpc-common.
@grpc/grpc-js
@grpc/grpc-js is a pure JavaScript implementation of gRPC for Node.js. It offers similar functionality to the grpc package but is designed to be more compatible with modern JavaScript and TypeScript projects. It also has a more complex API compared to nice-grpc-common.
grpc-tools
grpc-tools is a package that provides tools for working with gRPC in Node.js, including code generation from .proto files. It complements the grpc and @grpc/grpc-js packages but does not provide the same level of abstraction and ease of use as nice-grpc-common.
nice-grpc-common
Common data structures and types for
nice-grpc
and nice-grpc-web
.
If you are making a middleware library, consider depending on
nice-grpc-common
, as it is considered more stable in terms of semver. Also,
this allows you to build an isomorphic client middleware (working on both
Node.js and the Browser).
For application code, use nice-grpc
or nice-grpc-web
directly. Both
re-export names from nice-grpc-common
.
Installation
npm install nice-grpc-common
Contents